﻿using AiXiu.BLL;
using AiXiu.Common;
using AiXiu.IBLL;
using AiXiu.Model;
using System;
using System.Web.UI;

namespace AiXiu.WebSite
{
    /// <summary>
    /// 登录页
    /// </summary>
    public partial class Login : Page
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string userName = txtUserName.Text;
                string password = txtPassword.Text;
                IUserManager userManager = new UserManager();
                OperResult<TBUsers> operResult = userManager.LoginByUserName(userName, password);
                if (operResult.StatusCode == StatusCode.Succeed)
                {
                    //缓存用户资料
                    TBUsers user = operResult.ReadResult();
                    IdentityManager.SaveUser(user);

                    //forms身份认证，返回路径直接跳转
                    //this.Redirect("loginsuccess", "Default.aspx");

                    //页面跳转，获取浏览器中的跳转路径，如果不为空，跳转到浏览器中的跳转路径，否则跳转到默认页面
                    string returnUrl = Request.QueryString["ReturnUrl"];
                    if (!string.IsNullOrEmpty(returnUrl))
                    {

                        this.Redirect("loginsuccess", returnUrl);
                    }
                    else
                    {
                        this.Redirect("loginsuccess", "Default.aspx");
                    }

                }
                else
                {
                    this.Alert("lgnfail", operResult.Message ?? "操作失败！");
                }
            }
        }
    }
}